package cn.huyanbing.visual.spring.research.registrar;

import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @BelongProject middleware
 * @BelongPackage cn.huyanbing.visual.spring.research.registrar
 * @Description  动态加入bean  GreetingServiceRegistrar 参考：
 * https://zhuanlan.zhihu.com/p/685703903
 *
 * @ClassName GreetingServiceRegistrar
 * @Author huyangbing
 * @Date 2024/11/3 22:19
 * @Version 1.0.0
 */
public class GreetingServiceRegistrar implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        // 动态注册 EnglishGreetingService
        GenericBeanDefinition englishGreetingServiceDefinition = new GenericBeanDefinition();
        englishGreetingServiceDefinition.setBeanClassName(EnglishGreetingService.class.getName());
        registry.registerBeanDefinition("englishGreetingService", englishGreetingServiceDefinition);

        // 动态注册 SpanishGreetingService
        GenericBeanDefinition spanishGreetingServiceDefinition = new GenericBeanDefinition();
        spanishGreetingServiceDefinition.setBeanClassName(SpanishGreetingService.class.getName());
        registry.registerBeanDefinition("spanishGreetingService", spanishGreetingServiceDefinition);
    }
}